home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / system-config-printer / AdvancedServerSettings.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-10-28  |  12KB  |  408 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from gettext import gettext as _
  5. import gobject
  6. import gtk
  7. import os
  8. import socket
  9. import tempfile
  10. import time
  11. from errordialogs import *
  12.  
  13. class AdvancedServerSettingsDialog:
  14.     RESOURCE = '/admin/conf/cupsd.conf'
  15.     
  16.     def __init__(self, cupsconn, parent = None, on_apply = None):
  17.         self.cupsconn = cupsconn
  18.         self.on_apply = on_apply
  19.         self.handler_ids = { }
  20.         dialog = gtk.Dialog(_('Advanced Server Settings'), parent, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OK, gtk.RESPONSE_OK))
  21.         dialog.set_default_response(gtk.RESPONSE_OK)
  22.         dialog.set_border_width(6)
  23.         dialog.set_resizable(False)
  24.         dialog.set_has_separator(False)
  25.         self.connect(dialog, 'response', self.on_response)
  26.         self.dialog = dialog
  27.         frames_vbox = gtk.VBox(False, 6)
  28.         dialog.vbox.pack_start(frames_vbox, False, False, 0)
  29.         history_frame = gtk.Frame()
  30.         label = gtk.Label('<b>' + _('Job History') + '</b>')
  31.         label.set_use_markup(True)
  32.         history_frame.set_label_widget(label)
  33.         history_frame.set_shadow_type(gtk.SHADOW_NONE)
  34.         frames_vbox.pack_start(history_frame, False, False, 0)
  35.         align = gtk.Alignment(0.5, 0.5, 1, 1)
  36.         align.set_padding(0, 0, 12, 0)
  37.         history_frame.add(align)
  38.         vbox = gtk.VBox(False, 0)
  39.         align.add(vbox)
  40.         rb1 = gtk.RadioButton(None, _('Do not preserve job history'), False)
  41.         self.rbPreserveJobNone = rb1
  42.         rb2 = gtk.RadioButton(rb1, _('Preserve job history but not files'), False)
  43.         self.rbPreserveJobHistory = rb2
  44.         rb3 = gtk.RadioButton(rb1, _('Preserve job files (allow reprinting)'), False)
  45.         self.rbPreserveJobFiles = rb3
  46.         vbox.pack_start(rb1, False, False, 0)
  47.         vbox.pack_start(rb2, False, False, 0)
  48.         vbox.pack_start(rb3, False, False, 0)
  49.         browse_frame = gtk.Frame()
  50.         label = gtk.Label('<b>' + _('Browse Servers') + '</b>')
  51.         label.set_use_markup(True)
  52.         browse_frame.set_label_widget(label)
  53.         browse_frame.set_shadow_type(gtk.SHADOW_NONE)
  54.         frames_vbox.pack_start(browse_frame, False, False, 0)
  55.         align = gtk.Alignment(0.5, 0.5, 1, 1)
  56.         align.set_padding(0, 0, 12, 0)
  57.         browse_frame.add(align)
  58.         vbox = gtk.VBox(False, 6)
  59.         align.add(vbox)
  60.         label = gtk.Label(_('Usually print servers broadcast their queues.  Specify print servers below to periodically ask for queues instead.'))
  61.         label.set_line_wrap(True)
  62.         vbox.pack_start(label, False, False, 0)
  63.         hbox = gtk.HBox(False, 6)
  64.         vbox.pack_start(hbox, False, False, 0)
  65.         scrollwin = gtk.ScrolledWindow()
  66.         scrollwin.set_shadow_type(gtk.SHADOW_IN)
  67.         scrollwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
  68.         hbox.pack_start(scrollwin, True, True, 0)
  69.         treeview = gtk.TreeView()
  70.         treeview.set_headers_visible(False)
  71.         scrollwin.add(treeview)
  72.         selection = treeview.get_selection()
  73.         selection.set_mode(gtk.SELECTION_MULTIPLE)
  74.         self.connect(selection, 'changed', self.on_treeview_selection_changed)
  75.         col = gtk.TreeViewColumn('', gtk.CellRendererText(), text = 0)
  76.         treeview.append_column(col)
  77.         self.browse_treeview = treeview
  78.         bb = gtk.VButtonBox()
  79.         bb.set_layout(gtk.BUTTONBOX_START)
  80.         hbox.pack_start(bb, False, False, 0)
  81.         add = gtk.Button(stock = gtk.STOCK_ADD)
  82.         bb.add(add)
  83.         self.connect(add, 'clicked', self.on_add_clicked)
  84.         self.add = add
  85.         remove = gtk.Button(stock = gtk.STOCK_REMOVE)
  86.         remove.set_sensitive(False)
  87.         bb.add(remove)
  88.         self.connect(remove, 'clicked', self.on_remove_clicked)
  89.         self.remove = remove
  90.         f = tempfile.TemporaryFile()
  91.         
  92.         try:
  93.             cupsconn.getFile(self.RESOURCE, file = f)
  94.         except cups.HTTPError:
  95.             s = None
  96.             show_HTTP_Error(s, dialog)
  97.             raise 
  98.  
  99.         
  100.         def parse_yesno(line):
  101.             arg1 = line.split(' ')[1].strip()
  102.             if arg1 in ('true', 'on', 'enabled', 'yes'):
  103.                 return True
  104.             if arg1 in ('false', 'off', 'disabled', 'no', '0'):
  105.                 return False
  106.             
  107.             try:
  108.                 if int(arg1) != 0:
  109.                     return True
  110.             except:
  111.                 arg1 in ('false', 'off', 'disabled', 'no', '0')
  112.                 arg1 in ('true', 'on', 'enabled', 'yes')
  113.  
  114.             raise RuntimeError
  115.  
  116.         preserve_job_history = True
  117.         preserve_job_files = False
  118.         browsing = True
  119.         self.browse_poll = []
  120.         f.seek(0)
  121.         for line in f.readlines():
  122.             l = line.lower()
  123.             if l.startswith('preservejobhistory '):
  124.                 
  125.                 try:
  126.                     preserve_job_history = parse_yesno(l)
  127.  
  128.                 continue
  129.             if l.startswith('preservejobfiles '):
  130.                 
  131.                 try:
  132.                     preserve_job_files = parse_yesno(l)
  133.  
  134.                 continue
  135.             if l.startswith('browsing '):
  136.                 
  137.                 try:
  138.                     browsing = parse_yesno(l)
  139.  
  140.                 continue
  141.             if l.startswith('browsepoll '):
  142.                 self.browse_poll.append(line[len('browsepoll '):].strip())
  143.                 continue
  144.         
  145.         if not browsing:
  146.             browse_frame.set_sensitive(False)
  147.         
  148.         if preserve_job_files:
  149.             self.rbPreserveJobFiles.set_active(True)
  150.         elif preserve_job_history:
  151.             self.rbPreserveJobHistory.set_active(True)
  152.         else:
  153.             self.rbPreserveJobNone.set_active(True)
  154.         self.preserve_job_history = preserve_job_history
  155.         self.preserve_job_files = preserve_job_files
  156.         model = gtk.ListStore(gobject.TYPE_STRING)
  157.         treeview.set_model(model)
  158.         for server in self.browse_poll:
  159.             model.append(row = [
  160.                 server])
  161.         
  162.         dialog.show_all()
  163.  
  164.     
  165.     def connect(self, widget, signal, handler, reason = None):
  166.         id = widget.connect(signal, handler)
  167.         if not self.handler_ids.has_key(reason):
  168.             self.handler_ids[reason] = []
  169.         
  170.         self.handler_ids[reason].append((widget, id))
  171.  
  172.     
  173.     def disconnect(self, reason = None):
  174.         for widget, id in self.handler_ids[reason]:
  175.             widget.disconnect(id)
  176.         
  177.         del self.handler_ids[reason]
  178.  
  179.     
  180.     def __del__(self):
  181.         self.dialog.destroy()
  182.  
  183.     
  184.     def on_treeview_selection_changed(self, selection):
  185.         self.remove.set_sensitive(selection.count_selected_rows() != 0)
  186.  
  187.     
  188.     def on_add_clicked(self, button):
  189.         model = self.browse_treeview.get_model()
  190.         iter = model.insert(0, row = [
  191.             _('Enter IP address')])
  192.         button.set_sensitive(False)
  193.         col = self.browse_treeview.get_columns()[0]
  194.         cell = col.get_cell_renderers()[0]
  195.         cell.set_property('editable', True)
  196.         self.browse_treeview.set_cursor((0,), col, start_editing = True)
  197.         self.connect(cell, 'edited', self.on_browse_poll_edited, 'edit')
  198.         self.connect(cell, 'editing-canceled', self.on_browse_poll_edit_cancel, 'edit')
  199.  
  200.     
  201.     def on_browse_poll_edited(self, cell, path, newvalue):
  202.         model = self.browse_treeview.get_model()
  203.         iter = model.get_iter(path)
  204.         model.set_value(iter, 0, newvalue)
  205.         cell.stop_editing(canceled = False)
  206.         cell.set_property('editable', False)
  207.         self.add.set_sensitive(True)
  208.         self.disconnect('edit')
  209.         valid = True
  210.         
  211.         try:
  212.             socket.getaddrinfo(newvalue, '0', socket.AF_UNSPEC, 0, 0, socket.AI_NUMERICHOST)
  213.         except socket.gaierror:
  214.             labels = newvalue.split('.')
  215.             seen_alpha = False
  216.             for label in labels:
  217.                 if label[0] == '-' or label.endswith('-'):
  218.                     valid = False
  219.                     break
  220.                 
  221.                 for char in label:
  222.                     if not seen_alpha:
  223.                         if char.isalpha():
  224.                             seen_alpha = True
  225.                         
  226.                     
  227.                     if not char.isalpha() and char.isdigit() or char == '-':
  228.                         valid = False
  229.                         break
  230.                         continue
  231.                 
  232.                 if not valid:
  233.                     break
  234.                     continue
  235.             
  236.             if valid and not seen_alpha:
  237.                 valid = False
  238.             
  239.         except:
  240.             not seen_alpha
  241.  
  242.         if valid:
  243.             count = 0
  244.             i = model.get_iter_first()
  245.             while i:
  246.                 if model.get_value(i, 0) == newvalue:
  247.                     count += 1
  248.                     if count == 2:
  249.                         valid = False
  250.                         selection = self.browse_treeview.get_selection()
  251.                         selection.select_iter(i)
  252.                         break
  253.                     
  254.                 
  255.                 i = model.iter_next(i)
  256.         else:
  257.             model.remove(iter)
  258.  
  259.     
  260.     def on_browse_poll_edit_cancel(self, cell):
  261.         cell.stop_editing(canceled = True)
  262.         cell.set_property('editable', False)
  263.         model = self.browse_treeview.get_model()
  264.         iter = model.get_iter((0,))
  265.         model.remove(iter)
  266.         self.add.set_sensitive(True)
  267.         self.remove.set_sensitive(False)
  268.         self.disconnect('edit')
  269.  
  270.     
  271.     def on_remove_clicked(self, button):
  272.         model = self.browse_treeview.get_model()
  273.         selection = self.browse_treeview.get_selection()
  274.         rows = selection.get_selected_rows()
  275.         refs = (map,)((lambda path: gtk.TreeRowReference(model, path)), rows[1])
  276.         for ref in refs:
  277.             path = ref.get_path()
  278.             iter = model.get_iter(path)
  279.             model.remove(iter)
  280.         
  281.  
  282.     
  283.     def on_response(self, dialog, response):
  284.         if response == gtk.RESPONSE_CANCEL or response != gtk.RESPONSE_OK:
  285.             self.disconnect()
  286.             del self
  287.             return None
  288.         preserve_job_files = self.rbPreserveJobFiles.get_active()
  289.         if not preserve_job_files:
  290.             pass
  291.         preserve_job_history = self.rbPreserveJobHistory.get_active()
  292.         model = self.browse_treeview.get_model()
  293.         browse_poll = []
  294.         iter = model.get_iter_first()
  295.         while iter:
  296.             browse_poll.append(model.get_value(iter, 0))
  297.             iter = model.iter_next(iter)
  298.             continue
  299.             response != gtk.RESPONSE_OK
  300.         if set(browse_poll) == set(self.browse_poll) and preserve_job_files == self.preserve_job_files and preserve_job_history == self.preserve_job_history:
  301.             self.disconnect()
  302.             del self
  303.             return None
  304.         f = tempfile.TemporaryFile()
  305.         
  306.         try:
  307.             self.cupsconn.getFile(self.RESOURCE, file = f)
  308.         except cups.HTTPError:
  309.             preserve_job_history == self.preserve_job_history
  310.             s = preserve_job_history == self.preserve_job_history
  311.             show_HTTP_Error(s, dialog)
  312.             return None
  313.  
  314.         if preserve_job_files:
  315.             job_files_line = 'PreserveJobFiles Yes\n'
  316.         
  317.         for server in browse_poll:
  318.             browsepoll_lines += 'BrowsePoll %s\n' % server
  319.         
  320.         f.seek(0)
  321.         conf = tempfile.TemporaryFile()
  322.         wrote_preserve_history = wrote_preserve_files = False
  323.         wrote_browsepoll = False
  324.         has_browsepoll = False
  325.         lines = f.readlines()
  326.         for line in lines:
  327.             l = line.lower().strip()
  328.             if l.startswith('browsepoll '):
  329.                 has_browsepoll = True
  330.                 break
  331.                 continue
  332.         
  333.         for line in lines:
  334.             l = line.lower().strip()
  335.             if l.startswith('preservejobhistory '):
  336.                 if wrote_preserve_history:
  337.                     continue
  338.                 
  339.                 line = job_history_line
  340.                 wrote_preserve_history = True
  341.             elif l.startswith('preservejobfiles '):
  342.                 if wrote_preserve_files:
  343.                     continue
  344.                 
  345.                 line = job_files_line
  346.                 wrote_preserve_files = True
  347.             elif has_browsepoll and l.startswith('browsepoll '):
  348.                 if wrote_browsepoll:
  349.                     continue
  350.                 
  351.                 conf.write(browsepoll_lines)
  352.                 wrote_browsepoll = True
  353.                 continue
  354.             elif not has_browsepoll and l.startswith('browsing '):
  355.                 if not wrote_browsepoll:
  356.                     conf.write(line)
  357.                     conf.write(browsepoll_lines)
  358.                     wrote_browsepoll = True
  359.                     continue
  360.                 
  361.             
  362.             conf.write(line)
  363.         
  364.         if not wrote_preserve_history:
  365.             conf.write(job_history_line)
  366.         
  367.         if not wrote_preserve_files:
  368.             conf.write(job_files_line)
  369.         
  370.         if not wrote_browsepoll:
  371.             conf.write(browsepoll_lines)
  372.         
  373.         conf.flush()
  374.         fd = conf.fileno()
  375.         os.lseek(fd, 0, os.SEEK_SET)
  376.         
  377.         try:
  378.             self.cupsconn.putFile('/admin/conf/cupsd.conf', fd = fd)
  379.         except cups.HTTPError:
  380.             s = None
  381.             show_HTTP_Error(s, dialog)
  382.             return None
  383.  
  384.         time.sleep(1)
  385.         
  386.         try:
  387.             attempt = 1
  388.             while attempt <= 5:
  389.                 
  390.                 try:
  391.                     self.cupsconn._connect()
  392.                 continue
  393.                 except RuntimeError:
  394.                     time.sleep(1)
  395.                     attempt += 1
  396.                     continue
  397.                 
  398.  
  399.                 None<EXCEPTION MATCH>RuntimeError
  400.         except AttributeError:
  401.             pass
  402.  
  403.         self.disconnect()
  404.         self.on_apply()
  405.         del self
  406.  
  407.  
  408.